package com.qianchang.microfinance.view.mine.loan;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.google.gson.reflect.TypeToken;
import com.qianchang.microfinance.bean.LoanProductBean;
import com.qianchang.microfinance.bean.PageBean;
import com.qianchang.microfinance.bean.event.AgreementEvent;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.base.BaseRecyclerViewFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;

/**
 * Created by jinbailiang on 2017/8/15.
 * 学生贷 我的借款item
 */

public class MyLoanStudentFragment extends BaseRecyclerViewFragment {

    private int productType;
    private String urlType;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            productType = bundle.getInt(ConstantAppInfo.PROJECT_TYPE);
            urlType = bundle.getString(MyLoanActivity.URL_TYPE);
        }
        EventBus.getDefault().register(this);
    }


    @Override
    protected void requestData(HttpParams params) {
        params.put("opSource", 1);
        params.put("maxResults", 10);
        params.put("productType", productType);
        DMOKHttpUtils.getInstance().post(mContext, DMConstant.Config.BASE_USER + "/loan/" + urlType + DMConstant.Config.V + "/list.do", params, mCallBack);
    }

    @Override
    protected BaseRecyclerAdapter getRecyclerAdapter() {
        return new MyLoanAdapter(mContext);
    }

    @Override
    protected Type getType() {
        return new TypeToken<PageBean<LoanProductBean>>() {
        }.getType();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAgreementSuccessed(AgreementEvent agreementEvent) {
        if (null != agreementEvent && agreementEvent.isSuccess()) {
            initData();
        }
    }


    @Override
    public void onItemClick(int position, long itemId) {
        super.onItemClick(position, itemId);
        Bundle bundle = new Bundle();
        LoanProductBean loanProductBean = ((MyLoanAdapter) mAdapter).getItems().get(position);
        bundle.putString(ConstantAppInfo.PROJECT_ID, loanProductBean.getProjectId());
        bundle.putString(ConstantAppInfo.PROJECT_UNIT, loanProductBean.getUnit());
        bundle.putString(ConstantAppInfo.PROJECT_STATUS, loanProductBean.getStatus());
        bundle.putString(ConstantAppInfo.PROJECT_STATUS_DES, loanProductBean.getStatusDesc());
        bundle.putInt(ConstantAppInfo.PROJECT_TYPE, productType);
        startActivity(MyLoanDetailActivity.class, bundle);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }
}
